unit GlobalLogger;

interface

uses
  System.Classes, System.SysUtils, FileLogger, CriticalSectionEx;

type

  TGlobalLogger = class
  public
    m_bLog: Boolean;
    m_bCapabilityLog: Boolean;
    m_bBusinessLog: Boolean;
    m_bNetDataPackLog: Boolean;
    m_LogFileList: TStringList;
    m_GlobalLoggerLock: TCriticalSectionEx;
  published
    property Log: Boolean read m_bLog write m_bLog;
    property CapabilityLog: Boolean read m_bCapabilityLog write m_bCapabilityLog;
    property BusinessLog: Boolean read m_bBusinessLog write m_bBusinessLog;
    property NetDataPackLog: Boolean read m_bNetDataPackLog write m_bNetDataPackLog;
  private
    m_FileLogger: TFileLogger;
  published
    property FileLogger: TFileLogger read m_FileLogger;
  public
    procedure AppendDisplayMsg(const sMsg: string; const Args: array of const);
    procedure AppendLogMessage(const sMsg: string; const Args: array of const);
    procedure AppendErrorLogMessage(const sMsg: string; const Args: array of const);
    procedure AppendBusinessMessage(const sMsg: string; const Args: array of const);
  public
    constructor Create;
    destructor Destroy; override;
  end;

var
  _GlobalLogger: TGlobalLogger;

implementation

constructor TGlobalLogger.Create;
begin
  inherited Create;
  m_bLog := TRUE;
  m_bCapabilityLog := TRUE;
  m_bBusinessLog := TRUE;
  m_bNetDataPackLog := TRUE;
  m_FileLogger := TFileLogger.Create;
  m_LogFileList := TStringList.Create;
  m_GlobalLoggerLock := TCriticalSectionEx.Create;
end;

destructor TGlobalLogger.Destroy;
begin
  FreeAndNil(m_LogFileList);
  FreeAndNil(m_FileLogger);
  FreeAndNil(m_GlobalLoggerLock);
  inherited Destroy;
end;

procedure TGlobalLogger.AppendDisplayMsg(const sMsg: string; const Args: array of const);
begin
  m_GlobalLoggerLock.Lock;
  m_LogFileList.Add(Format(sMsg, Args));
  m_GlobalLoggerLock.UnLock;
end;

procedure TGlobalLogger.AppendLogMessage(const sMsg: string; const Args: array of const);
begin
  (* 是否写日志       *)
  if m_bLog and m_FileLogger.FbInit then m_FileLogger.WriteLogMsg(sMsg, Args);
end;

procedure TGlobalLogger.AppendErrorLogMessage(const sMsg: string; const Args: array of const);
begin
  (* 是否写性能日志   *)
  if m_bCapabilityLog and m_FileLogger.FbInit then m_FileLogger.WriteErrorLogMsg(sMsg, Args);
end;

procedure TGlobalLogger.AppendBusinessMessage(const sMsg: string; const Args: array of const);
begin
  (* 是否写业务日志   *)
  if m_bBusinessLog and m_FileLogger.FbInit then m_FileLogger.WriteBusinessMsg(sMsg, Args);
end;

initialization
  _GlobalLogger := TGlobalLogger.Create;

finalization
  FreeAndNil(_GlobalLogger);

end.

